﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data;
using VAdvantage.DataBase;
using System.ComponentModel;
using VAdvantage.Model;
using VAdvantage.Classes;
using System.Collections.ObjectModel;
using VAdvantage.ExcelImportService;

namespace VAdvantage.ImpExp
{
    public partial class MapLocation : ChildWindow
    {
        public MapLocation()
        {
            InitializeComponent();
            btnMap.Click += new RoutedEventHandler(btnMap_Click);
            btnUnMap.Click += new RoutedEventHandler(btnUnMap_Click);

        }

        protected override void OnOpened()
        {
            lstLocationMap.Items.Clear();

            if (mappingDetail.LocationMapping != null)
            {
                for (int a = 0; a < mappingDetail.LocationMapping.Count; a++)
                {
                    if (!mappingDetail.LocationMapping[a].ToDelete)
                    {
                        ExcelMappingDetail detail = mappingDetail.LocationMapping[a];
                        lstLocationMap.Items.Add(detail.AD_Column_Name + "<===>" + detail.ExcelColName);
                        m_MappedColumns.Add(detail.AD_Column_ID);
                    }
                }
            }
        }

        void btnUnMap_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int index = lstLocationMap.SelectedIndex;

                if (index < 0)
                    return;

                m_MappedColumns.RemoveAt(index);
                m_ExcelColIndex.RemoveAt(index);
                MappedList.RemoveAt(index);
                string colname = m_MappedColumnName[index];
                m_MappedColumnName.RemoveAt(index);

                for (int i = 0; i < mappingDetail.LocationMapping.Count; i++)
                {
                    if (colname.Equals(mappingDetail.LocationMapping[i].AD_Column_Name))
                    {
                        mappingDetail.LocationMapping[i].ToDelete = true;
                    }
                }
                lstLocationMap.Items.RemoveAt(index);
            }
            catch
            {
            }
        }

        List<int> m_MappedColumns = new List<int>();

        public List<int> MappedColumns
        {
            get { return m_MappedColumns; }
            set { m_MappedColumns = value; }
        }

        List<String> m_MappedColumnName = new List<String>();

        public List<String> MappedColumnName
        {
            get { return m_MappedColumnName; }
            set { m_MappedColumnName = value; }
        }

        List<int> m_ExcelColIndex = new List<int>();

        public List<int> ExcelColIndex
        {
            get { return m_ExcelColIndex; }
            set { m_ExcelColIndex = value; }
        }

        List<String> m_MappedList = new List<String>();

        public List<String> MappedList
        {
            get { return m_MappedList; }
            set { m_MappedList = value; }
        }

        void btnMap_Click(object sender, RoutedEventArgs e)
        {
            if (lstDBCols.SelectedIndex < 0)
            {
                ShowMessage.Info("SelectDBColumn", false, "", "");
                return;
            }

            if (lstExcelCols.SelectedIndex < 0)
            {
                ShowMessage.Info("SelectExcelColumn", false, "", "");
                return;
            }


            int _selected_col_id = (lstDBCols.SelectedItem as KeyNamePair).GetKey();

            if (m_MappedColumns.Contains(_selected_col_id))
            {
                ShowMessage.Info("AlreadyMapped", false, "", "");
                return;
            }



            m_MappedColumns.Add(_selected_col_id);
            m_ExcelColIndex.Add(lstExcelCols.SelectedIndex);

            String mapText = (lstDBCols.SelectedItem as KeyNamePair).GetName();

            ExcelMappingDetail excel = new ExcelMappingDetail()
            {
                AD_Column_ID = _selected_col_id,
                ExcelColIndex = lstExcelCols.SelectedIndex,
                AD_Column_Name = mapText,
                ExcelColName = lstExcelCols.SelectedItem.ToString(),
                UniqueKey = lstDBCols.SelectedIndex,
                IsIdentifier = false,
            };

            if (mappingDetail.LocationMapping == null)
                mappingDetail.LocationMapping = new List<ExcelMappingDetail>();
            bool chk = true;
            for (int i = 0; i < mappingDetail.LocationMapping.Count; i++)
            {
                if (excel.AD_Column_ID == mappingDetail.LocationMapping[i].AD_Column_ID)
                {
                    //  tabdetail.ColumnDetail.RemoveAt(i);
                    // tabdetail.ColumnDetail[i].ExcelColName = text;
                    mappingDetail.LocationMapping[i].ToDelete = false;
                    chk = false;
                    break;
                }
            }
            if (chk)
            {
                mappingDetail.LocationMapping.Add(excel);
            }
            //mappingDetail.LocationMapping.Add(excel);

            m_MappedColumnName.Add(mapText);

            mapText += "<===>";
            mapText += (lstExcelCols.SelectedItem.ToString());

            MappedList.Add(mapText);

            lstLocationMap.Items.Add(mapText);

        }

        ExcelMappingPerTabDetail mappingDetail;

        public ExcelMappingPerTabDetail MappingDetail
        {
            get { return mappingDetail; }
            set { mappingDetail = value; }
        }

        /// <summary>
        /// Standard Constructor
        /// </summary>
        /// <param name="Ad_Window_ID"></param>
        /// <param name="DBCols"></param>
        /// <param name="ExcelCols"></param>
        /// <param name="_mappingDetail"></param>
        public MapLocation(int Ad_Window_ID, Dictionary<int, String> DBCols, List<String> ExcelCols, ExcelMappingPerTabDetail _mappingDetail)
        {
            mappingDetail = _mappingDetail;
            InitializeComponent();

            btnMap.Click += new RoutedEventHandler(btnMap_Click);
            btnUnMap.Click += new RoutedEventHandler(btnUnMap_Click);

            loadWindowsDetails.IsBusy = true;

            foreach (KeyValuePair<int, String> pair in DBCols)
            {
                lstDBCols.Items.Add(new KeyNamePair(pair.Key, pair.Value));
            }

            if (ExcelCols != null)
            {
                foreach (String s in ExcelCols)
                {
                    lstExcelCols.Items.Add(s);
                }
            }

            loadWindowsDetails.IsBusy = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}

